#include "stdafx.h"
#include "SpriteMultiFrame.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

SpriteMultiFrame::SpriteMultiFrame(LPDIRECT3DTEXTURE9 tex, int nCol,int nWidth,int nHeight,int nFrameSpan, int nNumOfFrame,int nLayer,int nOnlyDraw1Line,bool bDrawOnView,bool bCycleMap):
SpriteSingleFrame(tex,nWidth,nHeight,nLayer,bDrawOnView,bCycleMap),m_nCol(nCol),m_nFrameSpan(nFrameSpan),m_nNumOfFrame(nNumOfFrame),m_nOnlyDraw1Line(nOnlyDraw1Line)
{
	m_nCurrentFrame=0;
	m_fAnimationTimer=0;
	m_nLastTime=0;
}

SpriteMultiFrame::~SpriteMultiFrame()
{

}

bool SpriteMultiFrame::Draw(DWORD dwCurrentTime)
{
	if(!SpriteSingleFrame::Draw(dwCurrentTime)){
		return false;
	}

	DWORD nElpasedTime=dwCurrentTime-m_nLastTime;
	m_fAnimationTimer += nElpasedTime;   

	if( m_fAnimationTimer > m_nFrameSpan ){   
		++m_nCurrentFrame;   

		if( m_nCurrentFrame > m_nNumOfFrame-1 ){
			m_nCurrentFrame = 0;
		}

		m_fAnimationTimer = 0;   
	}   
	m_nLastTime=dwCurrentTime;

	return true;
}

void SpriteMultiFrame::CalculateDrawingRect( RECT &srcRect )
{
	if(-1==m_nOnlyDraw1Line){
		srcRect.top = ( m_nCurrentFrame / m_nCol ) * m_nHeight;   
	}else{
		srcRect.top = m_nOnlyDraw1Line * m_nHeight;
	}
	srcRect.left   = ( m_nCurrentFrame % m_nCol ) * m_nWidth;
	srcRect.bottom = srcRect.top  + m_nHeight;
	srcRect.right  = srcRect.left + m_nWidth;
}